﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d_xna2.Classes;
using cocos2d;

namespace cocos2d_xna2.Classes
{
    public class FireBtn : CCLayer
    {
        GameLayer gLayer;
        tankAction tAct;
        CCSprite sprite;
        bool isActive;

        public GameLayer GLayer
        {
            set
            {
                gLayer = value;
            }
        }
        public static new FireBtn node()
        {
            FireBtn scene = new FireBtn();

            if (scene.init())
            {
                return scene;
            }
            else
            {
                return null;
            }
        }


        public void fireActive()
        {
            if (!isActive) 
            {
                isActive = true;
                sprite.runAction(CCFadeTo.actionWithDuration(0.5f, 255));

                CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, int.MinValue + 1, true);
                //schedule(keepDoing, 1 / 30);
            }
        }

        //public override void onEnter()
        //{
        //    base.onEnter();
        //}

        //public override void onExit()
        //{
        //    base.onExit();
        //}

        void keepDoing(float dt){
            gLayer.onTankAction(tAct);
        }

        public override bool init()
        {
            if (!base.init())
            {
                return false;
            }
            sprite = CCSprite.spriteWithFile(@"images/fire");
            addChild(sprite);
            sprite.position = CCPointExtension.ccp(400, 80);            
            sprite.Opacity = 0;
            isTouchEnabled = true;

            return true;
        }

        CCRect AtlasRect(CCSprite atlSpr)
        {
            //CCRect crect = new CCRect(
            //    atlSpr.position.x,
            //    atlSpr.position.y,
            //    atlSpr.Texture.getContentSize().width, 
            //    atlSpr.Texture.getContentSize().height);

            CCRect crect = atlSpr.textureRect;

            return ccMacros.CCRectMake(
                -crect.size.width / 2,
                -crect.size.height / 2,
                crect.size.width,
                crect.size.height);
        }

        public override bool ccTouchBegan(CCTouch touch, CCEvent event_)
        {
            sprite.runAction(CCFadeTo.actionWithDuration(0.1f, 250));

            CCPoint touchPoint = sprite.convertTouchToNodeSpaceAR(touch);
            CCRect tempRect = this.AtlasRect(sprite);
            if (CCRect.CCRectContainsPoint(tempRect, touchPoint))
            {
                tAct = tankAction.tFire;
                return true;
            }
            return false;
        }

        public override void ccTouchMoved(CCTouch touch, CCEvent event_)
        {
            sprite.runAction(CCFadeTo.actionWithDuration(0.1f, 250));

            CCPoint touchPoint = sprite.convertTouchToNodeSpaceAR(touch);
            CCRect tempRect = this.AtlasRect(sprite);
            if (CCRect.CCRectContainsPoint(tempRect, touchPoint))
            {
                tAct = tankAction.tFire;
                //keepDoing(0);
                schedule(keepDoing, 1 / 3);
            }
        }

        public override void ccTouchEnded(CCTouch touch, CCEvent event_)
        {
            tAct = tankAction.tStay;
            //sprite.runAction(CCFadeTo.actionWithDuration(0.1f, 50));
        }

        ~FireBtn()
        {
            //~base();
            System.Diagnostics.Debug.WriteLine("My game : FireBtn dealloc");
            
        }


    }
}
